<%--
    Document   : adminAddProject
    Created on : Jul 7, 2012, 2:08:48 PM
    Author     : ThanhTai
--%>

<%@page import="org.xml.sax.SAXException"%>
<%@page import="javax.xml.transform.sax.SAXSource"%>
<%@page import="org.xml.sax.InputSource"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="javax.xml.validation.Validator"%>
<%@page import="javax.xml.validation.Schema"%>
<%@page import="javax.xml.validation.SchemaFactory"%>
<%@page import="javax.xml.XMLConstants"%>
<%@page import="javax.xml.bind.JAXBContext"%>
<%@page import="javax.xml.bind.Unmarshaller"%>
<%@page import="util.SupportUtil"%>
<%@page import="DTO.Projects"%>
<%@page import="DTO.Date"%>
<%@page import="DTO.Project"%>
<%@page import="javax.xml.xpath.XPathConstants"%>
<%@page import="org.w3c.dom.Node"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.w3c.dom.NodeList"%>
<%@page import="javax.xml.xpath.XPath"%>
<%@page import="javax.xml.xpath.XPathFactory"%>
<%@page import="java.io.File"%>
<%@page import="org.w3c.dom.Document"%>
<%@page import="javax.xml.parsers.DocumentBuilder"%>
<%@page import="javax.xml.parsers.DocumentBuilderFactory"%>
<%@page import="DTO.Users"%>
<%@page import="BLO.UserBLO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>

<style type="text/css">
    @import url("css/general.css");
</style>
<script type="text/javascript">
    function validateForm()
    {
        var form = document.forms["InputProject"];

        var txtProjectName = form.txtProjectName.value;
        var ddlProjectManager = form.ddlProjectManager.value;
        var txtNumofMinestone = form.txtNumofMinestone.value;
        var SDyear = form.SDyear.value;
        var SDmonth = form.SDmonth.value;
        var SDday = form.SDday.value;
        var EDyear = form.EDyear.value;
        var EDmonth = form.EDmonth.value;
        var EDday = form.EDday.value;

        if (txtProjectName==""){
            window.alert("ProjectName NOT EMPTY");
            return false;
        }
        else if (ddlProjectManager==""){
            window.alert("ProjectManager NOT EMPTY");
            return false;
        }else if (txtNumofMinestone==""){
            window.alert("NumofMinestone NOT EMPTY");
            return false;
        }else if(SDyear > EDyear){
            window.alert("Start date must be less than end date !!!");
            return false;
        }else if(SDyear == EDyear){
            if(SDmonth > EDmonth){
                window.alert("Start date must be less than end date !!!");
                return false;
            }else if(SDmonth == EDmonth){
                if(SDday > EDday){
                    window.alert("Start date must be less than end date !!!");
                    return false;
                }
            }
        }

        var ValidChars = "0123456789";
        var IsNumber=true;
        var Char;
        for (i = 0; i < txtNumofMinestone.length && IsNumber == true; i++)
        {
            Char = txtNumofMinestone.charAt(i);
            if (ValidChars.indexOf(Char) == -1)
            {
                IsNumber = false;
                window.alert("Num Of Minestone must be a number !!!");
            }
        }
        return IsNumber;
    }
</script>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <jsp:useBean id="projectSAXBean" class="DTO.ProjectAddSAXProcessor"/>
        <%
                    UserBLO blo = new UserBLO();
                    String fileName = application.getRealPath("/") + "xml/Users.xml";
                    blo.getAllUser(fileName);
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db;
                    db = dbf.newDocumentBuilder();
                    Document doc;
                    doc = db.parse(new File(fileName));
                    XPathFactory xps = XPathFactory.newInstance();
                    XPath xp = xps.newXPath();
                    String exp = "//user[normalize-space(userRole)='pm']";
                    NodeList node = (NodeList) xp.evaluate(exp, doc, XPathConstants.NODESET);

                    ArrayList<String> listPM = new ArrayList<String>();
                    NodeList projectNode = null;
                    Node children = null;
                    for (int i = 0; i < node.getLength(); i++) {
                        projectNode = (NodeList) node.item(i);
                        for (int j = 0; j < projectNode.getLength(); j++) {
                            children = projectNode.item(j);
                            if (children.getNodeName().equals("username")) {
                                listPM.add(children.getTextContent().trim());
                            }
                        }
                    }
                    request.setAttribute("LISTPM", listPM);

                    String action = request.getParameter("action");
                    if (action != null) {
                        if (action.equals("AddProject")) {
                            String projectName = request.getParameter("txtProjectName");
                            String projectManager = request.getParameter("ddlProjectManager");
                            String Description = request.getParameter("txtDescription");
                            Integer NumOfMinestone = Integer.parseInt(request.getParameter("txtNumofMinestone"));
                            Integer sday = Integer.parseInt(request.getParameter("SDday"));
                            Integer smonth = Integer.parseInt(request.getParameter("SDmonth"));
                            Integer syear = Integer.parseInt(request.getParameter("SDyear"));
                            Integer eday = Integer.parseInt(request.getParameter("EDday"));
                            Integer emonth = Integer.parseInt(request.getParameter("EDmonth"));
                            Integer eyear = Integer.parseInt(request.getParameter("EDyear"));
                            String Status = request.getParameter("ddlStatus");
                            String filePath = application.getRealPath("/") + "xml/Projects.xml";
                            Date start = new Date(syear, smonth, sday);
                            Date end = new Date(eyear, emonth, eday);
                            Projects projects = new Projects();
                            Project project = new Project(projectName, projectManager, Description, NumOfMinestone, start, end, Status);
                            projects.add(project);
                            SupportUtil util = new SupportUtil();
                            util.marshal(filePath, projects);
                            try {
                                JAXBContext jc = JAXBContext.newInstance("util");
                                Unmarshaller u = jc.createUnmarshaller();
                                SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                                String pathProjectsXSD = application.getRealPath("/") + "xsd/Projects.xsd";
                                Schema schema = sf.newSchema(new File(pathProjectsXSD));
                                Validator validator = schema.newValidator();
                                InputSource inputFile = new InputSource(new BufferedReader(new FileReader(filePath)));
                                validator.validate(new SAXSource(inputFile));
                                projectSAXBean.save(filePath);
                                request.setAttribute("RESULT", "Add Project Successful");
                            } catch (SAXException ex) {
                                request.setAttribute("RESULT", "Error ");
                                ex.printStackTrace();
                            }
                            request.setAttribute("RESULT", "Add Project Successful");
                            RequestDispatcher rd = request.getRequestDispatcher("adminAddProject.jsp?action=AddProject1");
                            rd.forward(request, response);
                        }
                    }
        %>

        <div id="wrapper">
            <div id="header">
                <div id="user">
                    ${sessionScope.FULLNAME}, <a href="Controller?action=logout">Log out</a>
                </div>
            </div>
            <div id="body">
                <div id="title">
                    <h2 align="center">Add Projects</h2>
                </div>
                <div id="content">
                    <div id="left">
                        <table id="Function Reference:" width="200px">
                            <tr>
                                <th>Main Functions:</th>
                            </tr>
                            <tr>
                                <th><a href="adminMainPage.jsp">Add User</a></th>
                            </tr>
                            <tr>
                                <th><a href="adminAddProject.jsp">Add Project</a></th>
                            </tr>
                        </table>
                    </div>
                    <div id="right">
                        <div id="titleRight">
                        </div>
                        <div id="contenRight" >

                            <form action="adminAddProject.jsp" name="InputProject" onsubmit="return validateForm()">
                                <table border="1">
                                    <tbody>
                                        <tr>
                                            <td>Project Name:</td>
                                            <td><input type="text" name="txtProjectName" value="" /> </td>
                                        </tr>
                                        <tr>
                                            <td>Project Manager:</td>
                                            <td>
                                                <select name="ddlProjectManager">
                                                    <c:forEach items="${requestScope.LISTPM}" var="pm">
                                                        <option>${pm}</option>
                                                    </c:forEach>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Description:</td>
                                            <td><input type="text" name="txtDescription" value="" /></td>
                                        </tr>
                                        <tr>
                                            <td>Num of Minestone:</td>
                                            <td><input type="text" name="txtNumofMinestone" value="" /></td>
                                        </tr>
                                        <tr>
                                            <td>Start Day:</td>
                                            <td>
                                                <select name="SDyear" id="syearId" size="1" onchange="schange(this.form);">
                                                    <option value="2011" selected>2011</option>
                                                    <option value="2012">2012</option>
                                                    <option value="2013">2013</option>
                                                    <option value="2014">2014</option>
                                                    <option value="2015">2015</option>
                                                    <option value="2016">2016</option>
                                                    <option value="2017">2017</option>
                                                    <option value="2018">2018</option>
                                                    <option value="2019">2019</option>
                                                    <option value="2020">2020</option>
                                                    <option value="2021">2021</option>
                                                    <option value="2022">2022</option>
                                                </select>
                                                <select name="SDmonth" id="smonthId" size="1" onchange="schange(this.form);">
                                                    <option value="1">January</option>
                                                    <option value="2">February</option>
                                                    <option value="3">March</option>
                                                    <option value="4">April</option>
                                                    <option value="5">May</option>
                                                    <option value="6">June</option>
                                                    <option value="7">July</option>
                                                    <option value="8">August</option>
                                                    <option value="9">September</option>
                                                    <option value="10">October</option>
                                                    <option value="11">November</option>
                                                    <option value="12">December</option>
                                                </select>
                                                <select name="SDday" id="sdayId" size="1">
                                                    <option value="1" selected>01</option>
                                                    <option value="2">02</option>
                                                    <option value="3">03</option>
                                                    <option value="4">04</option>
                                                    <option value="5">05</option>
                                                    <option value="6">06</option>
                                                    <option value="7">07</option>
                                                    <option value="8">08</option>
                                                    <option value="9">09</option>
                                                    <option value="10">10</option>
                                                    <option value="11">11</option>
                                                    <option value="12">12</option>
                                                    <option value="13">13</option>
                                                    <option value="14">14</option>
                                                    <option value="15">15</option>
                                                    <option value="16">16</option>
                                                    <option value="17">17</option>
                                                    <option value="18">18</option>
                                                    <option value="19">19</option>
                                                    <option value="20">20</option>
                                                    <option value="21">21</option>
                                                    <option value="22">22</option>
                                                    <option value="23">23</option>
                                                    <option value="24">24</option>
                                                    <option value="25">25</option>
                                                    <option value="26">26</option>
                                                    <option value="27">27</option>
                                                    <option value="28">28</option>
                                                    <option value="29">29</option>
                                                    <option value="30">30</option>
                                                    <option value="31">31</option>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>End Day:</td>
                                            <td>
                                                <select name="EDyear" id="eyearId" size="1" onchange="echange(this.form);">
                                                    <option value="2011" selected>2011</option>
                                                    <option value="2012">2012</option>
                                                    <option value="2013">2013</option>
                                                    <option value="2014">2014</option>
                                                    <option value="2015">2015</option>
                                                    <option value="2016">2016</option>
                                                    <option value="2017">2017</option>
                                                    <option value="2018">2018</option>
                                                    <option value="2019">2019</option>
                                                    <option value="2020">2020</option>
                                                    <option value="2021">2021</option>
                                                    <option value="2022">2022</option>
                                                </select>
                                                <select name="EDmonth" id="emonthId" size="1" onchange="echange(this.form);">
                                                    <option value="1">January</option>
                                                    <option value="2">February</option>
                                                    <option value="3">March</option>
                                                    <option value="4">April</option>
                                                    <option value="5">May</option>
                                                    <option value="6">June</option>
                                                    <option value="7">July</option>
                                                    <option value="8">August</option>
                                                    <option value="9">September</option>
                                                    <option value="10">October</option>
                                                    <option value="11">November</option>
                                                    <option value="12">December</option>
                                                </select>
                                                <select name="EDday" id="edayId" size="1">
                                                    <option value="1" selected>01</option>
                                                    <option value="2">02</option>
                                                    <option value="3">03</option>
                                                    <option value="4">04</option>
                                                    <option value="5">05</option>
                                                    <option value="6">06</option>
                                                    <option value="7">07</option>
                                                    <option value="8">08</option>
                                                    <option value="9">09</option>
                                                    <option value="10">10</option>
                                                    <option value="11">11</option>
                                                    <option value="12">12</option>
                                                    <option value="13">13</option>
                                                    <option value="14">14</option>
                                                    <option value="15">15</option>
                                                    <option value="16">16</option>
                                                    <option value="17">17</option>
                                                    <option value="18">18</option>
                                                    <option value="19">19</option>
                                                    <option value="20">20</option>
                                                    <option value="21">21</option>
                                                    <option value="22">22</option>
                                                    <option value="23">23</option>
                                                    <option value="24">24</option>
                                                    <option value="25">25</option>
                                                    <option value="26">26</option>
                                                    <option value="27">27</option>
                                                    <option value="28">28</option>
                                                    <option value="29">29</option>
                                                    <option value="30">30</option>
                                                    <option value="31">31</option>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Status Day:</td>
                                            <td>
                                                <select name="ddlStatus">
                                                    <option>on-going</option>
                                                    <option>finish</option>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <input type="submit" value="AddProject" name="action" />
                                            </td>
                                            <td>
                                                <input type="reset" value="Reset" />
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <h4 align="left" style="color: red;">${requestScope.RESULT}</h4>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div id="footer">
            </div>
        </div>
    </body>
</html>
